<?php
namespace app\manage\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class UnitBuilding extends Model
{
    /**
     * 单元楼添加时候显示的栋和单元
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitList(){
        $input=input();
        $create_id= create_id($input['token']);
        $data=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->where(['create_id'=>$create_id])
            ->field('tung_num')
            ->find();
        $date=Db::name('settings')
            ->where(['create_id'=>$create_id])
            ->where(['apartment_state'=>1])
            ->field('unit_num')
            ->find();
        //判断设置没有   没有返回2
        if($data!=null && $date!=null){
        $arr['tung']=$data['tung_num'];//栋
        $arr['unit']=$date['unit_num'];//单元
           return $arr;
        }else{
            if(empty($data['tung_num'])){
                return 3;
            }
            return 2;
        }


    }
    /**
     * 单元楼添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitAdd($data){
        //处理数据
        $arr['unit_name'] = $data['unit_name'];
        $arr['tung_num'] = $data['tung_num'];
        $arr['unit_num'] = $data['unit_num'];
        $arr['layer_num'] = $data['layer_num'];
        $arr['create_id'] = create_id($data['token']);
        $arr['area'] = $data['area'];
        $arr['floor_type']=json_encode($data['floor_type']);
       // var_dump($arr);exit;
        $arr['u_a_timg'] = time();//
        $where['tung_num']=$data['tung_num'];
        $where['create_id'] = create_id($data['token']);
        $where['unit_num']=$data['unit_num'];
        $i=Db::name('unit_building')->where(['u_type'=>1])->where($where)->find();
        if($i!=null){
            return false;
        }
        $data=Db::name('unit_building')->insert($arr);
        return $data;
    }
    /**
     * 单元楼显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitOne($id){
        //处理数据
        $data=Db::name('unit_building')
            ->field('unit_id,unit_name,tung_num,unit_num,layer_num,area,floor_type')
            ->where(['unit_id'=>$id])
            ->where(['u_type'=>1])
            ->find();
        $u= json_decode($data['floor_type']);
        $data['floor_type']=$u;
        return $data;
    }
    /**
     * 单元楼修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitUpdate($data){
        //处理数据
        $where['unit_id']=$data['unit_id'];
        $arr['unit_name'] = $data['unit_name'];
        $arr['tung_num'] = $data['tung_num'];
        $arr['unit_num'] = $data['unit_num'];
        $arr['layer_num'] = $data['layer_num'];
        $arr['area'] = $data['area'];
        $arr['floor_type']=json_encode($data['floor_type']);
      //  $arr['floor_type'] = implode(',',$data['floor_type']);
        $arr['u_u_time'] = time();//
        $data=Db::name('unit_building')->where($where)->update($arr);
        return $data;
    }
    /**
     * 单元楼显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitIndex(){
        $datas = input();
        $page=isset($datas['page']) ? trim($datas['page']) : 1;
        $where['u_type']=1;
        $where['create_id'] = create_id($datas['token']);
        $count=Db::name('unit_building')
            ->where($where)
            ->count();
        $data=Db::name('unit_building')
            ->field('unit_id,unit_name,tung_num,unit_num,layer_num,area,floor_type')
            ->where($where)
            ->page($page, 6)
            ->order('tung_num asc,unit_num asc,layer_num asc')
            ->select();
        foreach ($data as $k=>$v){
            //sum('')
            $i['h_tung_num']=$v['tung_num'];
            $i['h_unit_num']=$v['unit_num'];
            $i['h_state']=1;
            $i['create_id'] = create_id($datas['token']);
            $s['s_tung_num']=$v['tung_num'];
            $s['s_unit_num']=$v['unit_num'];
            $s['s_state']=1;
            $s['create_id'] = create_id($datas['token']);
            $data[$k]['h_area']= Db::name('housing_information')->where($i)->sum('h_area');//住房面积
            $data[$k]['h_number']= Db::name('housing_information')->where($i)->count();//住房数量
            $data[$k]['h_rented']= Db::name('housing_information')->where($i)->where(['lease'=>2])->count();//已入住住房数量
            $data[$k]['h_not_rented']= Db::name('housing_information')->where($i)->where(['lease'=>1])->count();//未入住住房数量

            $data[$k]['s_area']= Db::name('shops')->where($s)->sum('s_area');//商铺面积
            $data[$k]['s_number']= Db::name('shops')->where($s)->count();//商铺数量
            $data[$k]['s_rented']= Db::name('shops')->where($s)->where(['lease'=>2])->count();//已入住商铺数量
            $data[$k]['s_not_rented']= Db::name('shops')->where($s)->where(['lease'=>2])->count();//未入住商铺数量

        }
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }
    /**
     * 单元楼删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function UnitDel($id){
        $unit=Db::name('unit_building')
            ->where(['unit_id'=>$id])
            ->find();
        $i['h_tung_num']=$unit['tung_num'];
        $i['h_unit_num']=$unit['unit_num'];
        $i['create_id']=$unit['create_id'];
        $i['h_state']=1;
        $s['s_tung_num']=$unit['tung_num'];
        $s['s_unit_num']=$unit['unit_num'];
        $s['create_id']=$unit['create_id'];
        $s['s_state']=1;
        $h_number= Db::name('housing_information')->where($i)->count();//住房数量
        $s_number= Db::name('shops')->where($s)->count();//商铺数量
        $count=$h_number+$s_number;
        if($count>0){
            return false;
        }
        $data=Db::name('unit_building')
            ->where(['unit_id'=>$id])
            ->update(['u_type'=>2]);
        return $data;
    }
}